// pages/index/select_community/select_community.js
import {
  Toast
} from 'wuss-weapp';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAuthorization: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let than = this
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              app.globalData.wxUserInfo = res.userInfo
              than.login()
            }
          })
        }
      }
    })
  },

  login: function () {
    let than = this
    wx.login({
      success: res => {
        const accountInfo = wx.getAccountInfoSync();
        app.wxGetRequest("/wechart/mine/authorize", {
          js_code: res.code,
          applet_id: accountInfo.miniProgram.appId,
          name: app.globalData.wxUserInfo["nickName"],
          head_img: app.globalData.wxUserInfo["avatarUrl"]
        }).then(result => {
          app.globalData.userInfo = result.data.data;
          wx.setStorageSync('user_id', app.globalData.userInfo["user_id"]);
          wx.setStorageSync('people_id', app.globalData.userInfo["people_id"]);
          wx.setStorageSync('name', app.globalData.userInfo["name"]);
          if (!app.globalData.userInfo["people_id"]) {
            if (app.globalData.appType == 1) {
              than.onItemWP()
            } else if (app.globalData.appType == 2) {
              than.onItemXHL()
            } else {

            }
            return;
          }
          app.isBindCommunity().then(result => {
            if (app.globalData.appType == 1) {
              than.onItemWP()
            } else if (app.globalData.appType == 2) {
              than.onItemXHL()
            } else {

            }
          });
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
   * 望平社区
   */
  onItemWP: function () {
    if (this.data.isAuthorization) {
      wx.setStorage({
        key: "community",
        data: 1
      })
      app.globalData.appTitle = '望平社区'
      app.globalData.unitCode = '86.001.001.001.001.009.003'
      app.globalData.parentAreaCode = '86.51.01.08.09.04'
      wx.switchTab({
        url: '/pages/index/index',
      })
      app.wxGetRequest('/applet/associateArea', {
        user_id: app.globalData.userInfo["user_id"],
        area_code: app.globalData.parentAreaCode
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }

  },
  /**
   * 祥和里社区
   */
  onItemXHL: function () {
    if (this.data.isAuthorization) {
      wx.setStorage({
        key: "community",
        data: 2
      })
      app.globalData.appTitle = '祥和里社区'
      app.globalData.unitCode = '86.001.001.001.001.009.004'
      app.globalData.parentAreaCode = '86.51.01.08.09.03'
      wx.switchTab({
        url: '/pages/index/index',
      })
      app.wxGetRequest('/applet/associateArea', {
        user_id: app.globalData.userInfo["user_id"],
        area_code: app.globalData.parentAreaCode
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  }
})